package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/shang/shang_updownProduct")
public class updownProduct extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取ID
        String id = req.getParameter("id");

        if (id == null || id.equals("")){
            PrintWriter out = resp.getWriter();
            out.println("<script>location.href='/shang/shang_index';</script>");
            out.close();
            return;
        }

        //下架
        boolean result = AdminDao.allowProduct(Integer.parseInt(id));
        if(result){
            PrintWriter out = resp.getWriter();
            out.println("<script>alert('下架成功');location.href='/shang/shang_index';</script>");
            out.close();
        }else{
            PrintWriter out = resp.getWriter();
            out.println("<script>alert('下架失败');location.href='/shang/shang_index';</script>");
            out.close();
        }
    }
}
